package com.cs410.collabwriting.filters;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import com.cs410.collabwriting.tools.LogManager;

/**
 * Servlet Filter implementation class RequestLogFilter
 */
@WebFilter("*")
public class RequestLogFilter implements Filter {

    /**
     * Default constructor. 
     */
    public RequestLogFilter() {
    	
    }

	/**
	 * 
	 */
	public void destroy() {
		
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpReq = (HttpServletRequest) request;
		String logMessage = "[WEB_FILTER] Request from: |" + httpReq.getRemoteHost() + 
				"| :: JSESSIONID: |" + httpReq.getRequestedSessionId() + 
				"| :: RequestURL: |" + httpReq.getRequestURL() + "|";
		LogManager.logDebug( logMessage, LogManager.LOG_LEVEL_3 );
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {

	}

}
